/*
 * jQuery File Upload Plugin JS Example 8.9.1
 * https://github.com/blueimp/jQuery-File-Upload
 *
 * Copyright 2010, Sebastian Tschan
 * https://blueimp.net
 *
 * Licensed under the MIT license:
 * http://www.opensource.org/licenses/MIT
 */

/* global $, window */

$(function () {
    'use strict';

    // Initialize the jQuery File Upload widget:
    $('#fileupload').fileupload({
            // Uncomment the following to send cross-domain cookies:
            //xhrFields: {withCredentials: true},
            url: $('span#file_url').attr('url'),
            sequentialUploads:true,
            stop:function (e) {
                    $.blockUI({ 
                        message: '<h1>Adding Thumbnails, Completing.</h1>', 
                        timeout: 5000 
                    }); 

                setInterval(location.reload(),4000);
            },
            destroy: function (e, data) {
                    var that  = $(this).data('fileupload');
                    if ( typeof e.originalEvent.originalEvent == "undefined" ) {
                        // Click on the "Delete All Button"
                    } else {
                        // Click on the file delete button
                    }
                }
    });

    // Enable iframe cross-domain access via redirect option:
    $('#fileupload').fileupload(
        'option',
        'redirect',
        window.location.href.replace(
            /\/[^\/]*$/,
            '/cors/result.html?%s'
        )
    );
        // Load existing files:
        $('#fileupload').addClass('fileupload-processing');
        $.ajax({
            // Uncomment the following to send cross-domain cookies:
            //xhrFields: {withCredentials: true},
            url: $('#fileupload').fileupload('option', 'url'),
            dataType: 'json',
            context: $('#fileupload')[0]
        }).always(function () {
            $(this).removeClass('fileupload-processing');
        }).done(function (result) {
            $(this).fileupload('option', 'done').call(this, $.Event('done'), {result: result});
        });
});


//delete file
$('#fileupload').bind('fileuploaddestroy', function (e, data) {
    // e.preventDefault();
    var filename = data.url.substring(data.url.indexOf("=") + 1);
    $("#msgDialog").dialog('option', 'buttons', {
        "Confirm" : function() {
                
                if(typeof  target == "undefined")
                {
                    // window.location.href = theHREF;
                    console.log(e);
                    $.ajax({
                        type:'get',
                        data:'json',
                        url:filename
                    }).done(function(){
                        setInterval(location.reload(),1000);
                        $("#msgDialog").dialog("close");
                    });
                }else{
                    $(this).dialog("close");
                    // window.open(theHREF);
                }
            },
        "Cancel" : function() {
                $(this).dialog("close");
            }
        });
    $("#msgDialog").dialog('option','height',300);
    $('.ui-dialog-buttonpane').find('button').addClass('btn btn-mini btn-primary');
    $( '#msgDialog > div.content' ).text('Are you sure you want to delete this file from the server? Changes cannot be undone.');
    $( '#msgDialog > p' ).text( 'Confirm.' );
    $("#msgDialog").dialog("open");
});